<template>
  <div class="energy-index-wrap">
      <el-row>箱信息
      <el-form :model="qryForm" :inline="true" ref="qryForm" label-width="85px">
        <el-form-item label="箱号">
          <el-input v-model="qryForm.cntr"></el-input>
        </el-form-item>
        <el-form-item label="当前状态">
          <s-code :code.sync="qryForm.currentStat" flgEng="CURRENT_STAT"></s-code>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      </el-row>
      <el-table
        :data="table1Data"
        style="width: 100%"
        ref="table1"
        stripe
        @selection-change="onTable1Select"
        @row-click="tableRowClick"
        highlight-current-row>
        <el-table-column type="selection"></el-table-column>
        <el-table-column prop="cntr" label="箱号" width="90"></el-table-column>
        <el-table-column prop="cntrCorpCod" label="箱公司" width="70"></el-table-column>
        <el-table-column prop="currentStat" label="当前状态" width="70">
          <template slot-scope="scope">
            <s-code :code.sync="scope.row.currentStat" flgEng="CURRENT_STAT" type="span"></s-code>
          </template>
        </el-table-column>
        <el-table-column prop="cntrSizCod" label="尺寸" width="60"></el-table-column>
        <el-table-column prop="cntrTypCod" label="类型" width="60"></el-table-column>
        <el-table-column prop="eFId" label="空重" width="50"></el-table-column>
        <el-table-column prop="reefId" label="冷藏箱" width="70">
          <template slot-scope="scope">
            <s-code :code.sync="scope.row.reefId" flgEng="REEF_ID" type="span"></s-code>
          </template>
        </el-table-column>
        <el-table-column prop="tempSet" label="温度设置" width="70"></el-table-column>
        <el-table-column prop="cShipNam" label="船名" width="90"></el-table-column>
        <el-table-column prop="voyage" label="航次" width="70"></el-table-column>
        <el-table-column prop="tradeId" label="内外贸" width="90">
          <template slot-scope="scope">
            <s-code :code.sync="scope.row.tradeId" flgEng="TRADE_ID" type="span"></s-code>
          </template>
        </el-table-column>
        <el-table-column prop="iEId" label="进出口" width="90">
          <template slot-scope="scope">
            <s-code :code.sync="scope.row.iEId" flgEng="I_E_ID" type="span"></s-code>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="table1Data.length!==0"
        :page-size="table.page.size"
        :current-page.sync="table.page.current"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :page-sizes="[10,20,30,40,50,100]"
        layout="total, sizes, prev, pager,next, jumper"
        :total="table.page.total"
      ></el-pagination>
        <el-row>温度记录
          <el-form :model="qryForm2" :inline="true" ref="qryForm" label-width="85px">
            <el-form-item>
              <el-button icon="el-icon-plus" @click="onAdd()">插入</el-button>
              <el-button icon="el-icon-plus" @click="save()">保存</el-button>
            </el-form-item>
          </el-form>
        </el-row>
        <ux-grid
          ref="table2"
          :data="table2Data"
          show-overflow="tooltip"
          size="mini"
          border
          keep-source
          :highlightCurrentRow="false"
          :edit-config="{trigger: 'click', mode: 'cell'}">
          <ux-table-column field="eFId" title="空重" width="70"></ux-table-column>
          <ux-table-column field="tempSet" title="温度设置" width="90" edit-render>
            <template v-slot:edit="scope">
              <el-input v-model="scope.row.tempSet"></el-input>
            </template>
          </ux-table-column>
          <ux-table-column field="cntrStatCod" title="状态" width="75" edit-render>
            <template v-slot:edit="scope">
              <el-input v-model="scope.row.cntrStatCod"></el-input>
            </template>
          </ux-table-column>
          <ux-table-column field="tempCheck" title="检查温度" width="90" edit-render>
            <template v-slot:edit="scope">
              <el-input v-model="scope.row.tempCheck"></el-input>
            </template>
          </ux-table-column>
          <ux-table-column field="workTim" title="检查时间" width="140" edit-render>
            <template v-slot:edit="scope" edit-render>
              <el-date-picker style="width: 100%" v-model="scope.row.workTim" type="datetime" value-format="timestamp"></el-date-picker>
            </template>
            <template v-slot="{ row }">{{ formattingTime(row.workTim) }}</template>
          </ux-table-column>
          <ux-table-column field="recNam" title="录入员" width="75"></ux-table-column>
          <ux-table-column field="recTim" title="录入时间" width="140">
            <template slot-scope="scope">
              <span>{{formattingTime(scope.row.recTim)}}</span>
            </template>
          </ux-table-column>
          <ux-table-column title="操作" width="50">
            <template slot-scope="scope">
              <el-button @click="del(scope.row)" type="text" v-if="scope.row.id != null">删除</el-button>
            </template>
          </ux-table-column>
        </ux-grid>
        <el-pagination
          v-if="table2Data.length!==0"
          :page-size="table2.page.size"
          :current-page.sync="table2.page.current"
          @current-change="handleCurrentChange2"
          @size-change="handleSizeChange2"
          :page-sizes="[10,20,30,40,50,100]"
          layout="total, sizes, prev, pager,next, jumper"
          :total="table2.page.total"
        ></el-pagination>
  </div>
</template>

<script>
import api from '@/api/modules/yardMgmt/rf-cntr-record/index'
import SCode from '@/components/Select/s-code/index.vue'
import { formattingTime } from '@/utils/validate'
export default {
  // name: 'shipLinePort',
  data() {
    return {
      qryForm: {
        cntr: '',
        currentStat: '',
      },
      qryForm2: {
        begCyAreaNo: '',
        endCyAreaNo: '',
        begCyBayNo: '',
        endCyBayNo: '',
        cntrSizCod: '',
        cntrTypCod: '',
        cntrCorpCod: '',
      },
      table1Data: [],
      table2Data: [],
      selectedTable1Data: [], // table1已选数据
      selectedTable2Data: []  ,// table2已选数据
      table2originData: []  ,
      table: {
        data: [],
        page: {
          size: 10,
          current: 1,
          total: 0
        },
        height: 0,
      },
      table2: {
        data: [],
        page: {
          size: 10,
          current: 1,
          total: 0
        },
        height: 0,
      },
      row: {}
    }
  },
  components: {
    SCode,
  },
  methods: {
    /**
     * table1选择事件处理函数
     * @param {array} rows 已勾选的数据
     */
    onTable1Select(rows) {
        this.selectedTable1Data = [...rows];
    },

    /**
     * table2选择事件处理函数
     * @param {array} rows 已勾选的数据
     */
    onTable2Select(rows) {
        this.selectedTable2Data = [...rows];
    },

    /**
     * 添加按钮事件处理函数
     */
    async onAdd(row) {
      let record = {
        cntrNo:this.row.cntrNo,
        eFId:this.row.eFId,
        tempSet:this.row.tempSet,
        workTim:Date.parse(new Date()),
        recNam:JSON.parse(sessionStorage.getItem("userInfo")).userName}
      let { row: newRow } = await this.$refs.table2.insertRow(record, row)
      await this.$refs.table2.setActiveRow(newRow)
    },

    /*
     *  保存按钮
     * */
    async save() {
      for(let i=0;i<this.$refs.table2.getUpdateRecords().length;i++){
        this.$refs.table2.getUpdateRecords()[i]['updNam'] = JSON.parse(sessionStorage.getItem("userInfo")).userName
      }
      const params = this.$refs.table2.getRecordset().concat(this.$refs.table2.getUpdateRecords())
      const res = await api.save(params)
      if (res.code === '00000000') {
        this.search2()
        this.$message({
          message: res.data,
          type: 'success'
        })
      }
    },

    /*
     *  删除
     * */
    del(row) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
          let params = {id:row.id}
          let res = await api.delete(params)
          this.$message({
            message: res.data,
            type: 'success'
          })
          this.page()
      })
    },

    // 按钮查询
    search2() {
      this.table2.page.current = 1
      this.page()
    },

    /*
     *  查询冷藏箱温度记录
     */
    async page(){
      const { current, size } = this.table2.page
      const params = {...this.row, current, size}
      const res = await api.page(params)
      if (res.code === '00000000') {
        this.table2Data = res.data.records
        this.table2.page.total = res.data.total
      }
    },

    /*
     * 分页
     * */
    handleCurrentChange2(val) {
      this.table2.page.current = val
      this.page()
    },
    /*
     *  pageSize变更
     */
    handleSizeChange2(val) {
      this.table2.page.size = val
      this.page()
    },

    /* 格式化时间*/
    formattingTime(e) {
      return formattingTime(e)
    },

    // 按钮查询
    search() {
      this.table.page.current = 1
      this.queryRfRecordPage()
    },

    /*
     * 分页
     * */
    handleCurrentChange(val) {
      this.table.page.current = val
      this.queryRfRecordPage()
    },
    /*
     *  pageSize变更
     */
    handleSizeChange(val) {
      this.table.page.size = val
      this.queryRfRecordPage()
    },

    /*
     *  查询在场箱
     */
    async queryRfRecordPage(){
      const { current, size } = this.table.page
      const params = {...this.qryForm, current, size}
      const res = await api.queryRfRecordPage(params)
      if (res.code === '00000000') {
        this.table1Data = res.data.records
        this.table.page.total = res.data.total
      }
    },

    /*
     *  表格行点击
     */
    async tableRowClick(row, column, event){
      this.row = row
      this.page()
    },

  },

  mounted() {
  },
}
</script>

<style>
</style>
